#!/usr/bin/env python2.5
# encoding: utf-8
"""
This script can be used to launch a DASE server
"""

# ##############
# Import modules
# ##############

import sys
# Ajout du dossier classes/ au PATH, afin de pouvoir inclure les autres
# parties de DASE simplement
sys.path.insert(0, 'DASEServer')

from tempfile import mkstemp
from subprocess import call

from DASEServer import DASEServer

# ####################
# DASE SQL Server list
# ####################

sql_servers = [
  {
    'desc':      'SQLite in memory',
    'engine':    'sqlite3',
    'db_server': (':memory:',)
  },
  {
    'desc':      'SQLite in tmpfile',
    'engine':    'sqlite3',
    'db_server': (mkstemp()[1],)
  },
  {
    'desc':      'MySQL on Localhost',
    'engine':    'MySQL',
    'db_server': ('localhost', 'root', '', 'dase')
  }
  ]

# ###############################
# Select the server to connect to
# ###############################

try:
  sql_server = sql_servers[int(sys.argv[1])]
  
  del sql_server['desc']
except Exception, e:
  print "Usage : dase.py id, id being the id of one of the sql_servers"
  print "---"
  
  i = 0
  for server in sql_servers:
    print i, '-', server['desc']
    
    i += 1
  
  print '---'
  print
  
  raise e

# ##################
# Launch DASE Server
# ##################

server_address = ('localhost', 4000)

dase = DASEServer(server_address, **sql_server)

server_url = 'http://%s:%i/'%(server_address[0], server_address[1])

print 'Listening on port 4000 : <' + server_url + '>'

print 'Connected to SQL Server : %s'%(sql_server)

retcode = call(["open", server_url])

dase.serve_until_quit()
